package com.wu.viewpagerdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

/**
 * ViewPager, default is not loop. can not loop.
 * 
 * @author Administrator
 *
 */
public class ActivityMain extends ActivityGroup {

	private static final int COUNT_ADAPTER_LOOP = Integer.MAX_VALUE;

	private static final Class<?>[] TAB = new Class<?>[] { ActivityTab1.class,
			ActivityTab2.class, ActivityTab3.class };

	private static int lastVisible = -1;
	private static List<IViewPagerChangeListener> tabs = new ArrayList<IViewPagerChangeListener>();

	private static int adapterCount = COUNT_ADAPTER_LOOP;

	private static boolean isLoop = false;

	private ViewPager mViewPager;
	private PagerTitleStrip mTitleStrip;

	private LocalActivityManager mManager;

	private List<View> views = new ArrayList<View>();
	private List<String> titles = new ArrayList<String>();

	private PagerAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mManager = getLocalActivityManager();

		initTabContainer();

		mTitleStrip = (PagerTitleStrip) findViewById(R.id.pagertitle);

		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mViewPager.setOnPageChangeListener(new MyPageChangeListener());
		mAdapter = new MyViewPagerAdapter();
		mViewPager.setAdapter(mAdapter);

		if (isLoop) {
			adapterCount = COUNT_ADAPTER_LOOP;
			mViewPager.setCurrentItem(1000 * TAB.length);
		} else {
			adapterCount = TAB.length;
			mViewPager.setCurrentItem(0);
		}

		visible(0);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initTabContainer() {
		View tab1 = mManager.startActivity("tab1", new Intent(this, ActivityTab1.class)).getDecorView();
		View tab2 = mManager.startActivity("tab2", new Intent(this, ActivityTab2.class)).getDecorView();
		View tab3 = mManager.startActivity("tab3", new Intent(this, ActivityTab3.class)).getDecorView();

		tabs = new ArrayList<IViewPagerChangeListener>();
		if (mManager.getActivity("tab1") instanceof IViewPagerChangeListener) {
			tabs.add((IViewPagerChangeListener) mManager.getActivity("tab1"));
		}
		if (mManager.getActivity("tab2") instanceof IViewPagerChangeListener) {
			tabs.add((IViewPagerChangeListener) mManager.getActivity("tab2"));
		}
		if (mManager.getActivity("tab3") instanceof IViewPagerChangeListener) {
			tabs.add((IViewPagerChangeListener) mManager.getActivity("tab3"));
		}

		views = new ArrayList<View>();
		views.add(tab1);
		views.add(tab2);
		views.add(tab3);

		titles = new ArrayList<String>();
		titles.add("tab 1");
		titles.add("tab 2");
		titles.add("tab 3");
	}

	private void visible(int index) {
		if (index < 0 || index > tabs.size() - 1) {
			return;
		}
		tabs.get(index).onVisible();
		lastVisible = index;
	}

	private void invisible(int index) {
		if (index < 0 || index > tabs.size() - 1) {
			return;
		}
		tabs.get(index).onInvisible();
	}

	public void setPagerLoop(boolean loop) {
		if (isLoop != loop) {
			isLoop = loop;
			mViewPager.removeAllViews();
			if (loop) {
				adapterCount = COUNT_ADAPTER_LOOP;
				mViewPager.setCurrentItem(1000 * lastVisible, false);
			} else {
				adapterCount = TAB.length;
				mViewPager.setCurrentItem(lastVisible);
			}
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(1, 1, 1, "set loop");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (menu.findItem(1) != null) {
			menu.removeItem(1);
		}
		if (isLoop) {
			menu.add(1, 1, 1, "set not loop");
		} else {
			menu.add(1, 1, 1, "set loop");
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 1) {
			setPagerLoop(!isLoop);
		}
		return super.onOptionsItemSelected(item);
	}

	class MyViewPagerAdapter extends PagerAdapter {

		/*
		 * Set the ViewPager is not infinite loop:
		 * return TAB.length;
		 */
		@Override
		public int getCount() {
			return adapterCount;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			try {
				((ViewPager) container).removeView(views.get(position % TAB.length));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles.get(position % TAB.length);
		}

		@Override
		public Object instantiateItem(View container, int position) {
			try {
				((ViewPager) container).addView(views.get(position % TAB.length));
				return views.get(position % TAB.length);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}
	}

	class MyPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int state) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int index) {
			invisible(lastVisible);
			visible(index % TAB.length);
			lastVisible = index % TAB.length;
		}
	}

}
